package com.flipdf2.serialize.ppml.element;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;

public class PPMLMetadata {
	public static final String NAME = "METADATA";
	
	private List<PPMLDatum> datumList;
	private String creator;
    private String creationDate;
    private String identifier;
    private String target;
    
    public PPMLMetadata() {
    }
    
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (creator != null && creator.length() > 0)
			root.setAttribute("Creator", creator);
		if (creationDate != null)
			root.setAttribute("CreationDate", creationDate);
		if (identifier != null && identifier.length() > 0)
			root.setAttribute("Identifier", identifier);
		if (target != null && target.length() > 0)
			root.setAttribute("Target", target);
		
		if (datumList != null && datumList.size() > 0) {
			for (int i = 0, n = datumList.size(); i < n; i++) {
				PPMLDatum datum = datumList.get(i);
				root.addContent(datum.toXML(defaultNamespace));				
			}
		}
		
		return root;
	}
	
	public void check() throws PPMLException {
		if (creator == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Creator");
		}
	}


	public String getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(String creationDate) {
		this.creationDate = creationDate;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public List<PPMLDatum> getDatumList() {
		return datumList;
	}

	public void setDatumList(List<PPMLDatum> datumList) {
		this.datumList = datumList;
	}

	public void addDatum(PPMLDatum datum) {
		if (this.datumList == null)
			this.datumList = new ArrayList<PPMLDatum>();
		this.datumList.add(datum);
	}

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}
}
